package com.yjt.sousou.detail;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yjt.sousou.R;
import com.yjt.sousou.detail.entity.FilterBean;

import java.util.List;

/**
 * 作者：83734
 * 时间：2018/11/25
 * 描述：
 */
public class DispatchPop extends PopupWindow {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<FilterBean> lists;
    private GetIdsListener mListener;
    DispatchPop(Context context, List<FilterBean> listBeans, float offsetX, GetIdsListener listener) {
        super(context);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        lists = listBeans;
        mListener = listener;
        View popView = mInflater.inflate(R.layout.pop_my_window, null);
        ListView lv_rooms =  popView.findViewById(R.id.lv_rooms);
        final ListAdapter adapter = new ListAdapter(context,listBeans,offsetX);
        lv_rooms.setAdapter(adapter);
        LinearLayout ll_main = popView.findViewById(R.id.ll_main);
        ll_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setContentView(popView);
        setFocusable(false);
        setAnimationStyle(R.style.PopupWindowAnimation);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        lv_rooms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < lists.size(); i++) {
                    lists.get(i).setClicked(position == i);
                }
                adapter.notifyDataSetChanged();
                mListener.ids(lists.get(position).getId());
                dismiss();
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if(mListener != null){
                    mListener.dismiss();
                }
            }
        });
    }

    public void show(final View anchor, final int xoff, final int yoff) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
            showAsDropDown(anchor, xoff, yoff);
        } else {
            showAsDropDown(anchor, xoff, yoff);
        }
    }
    private class ListAdapter extends MyBaseAdapter<FilterBean ,ListView> {
        float offsetX;
        ListAdapter(Context ct, List<FilterBean> list, float offsetX) {
            super(ct, list);
            this.offsetX = offsetX;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_my_pop_list, null);
                holder = new ViewHolder();
                holder.mTvRoomName = convertView.findViewById(R.id.tv_name);
                holder.mRlItem = convertView.findViewById(R.id.rl_item);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            FilterBean item = list.get(position);
            holder.mTvRoomName.setText(item.getName());
            holder.mRlItem.setPadding((int) offsetX,0,0,0);
            if(item.isClicked()){
                holder.mTvRoomName.setTextColor(ContextCompat.getColor(mContext,R.color.blue_bg));
            }else {
                holder.mTvRoomName.setTextColor(ContextCompat.getColor(mContext,R.color.black));
            }
            return convertView;
        }
    }
    static class ViewHolder {
        RelativeLayout mRlItem;
        TextView mTvRoomName;
    }
    public interface GetIdsListener{
        void ids(String id);
        void dismiss();
    }
}
